<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handlePrint({})" v-auth="'bas:pos:basPosition:edit'">
          <Icon icon="simple-line-icons:printer" /> {{ t('标签打印') }}
        </a-button>
      </template>
      <!-- 表格插槽 -->
      <template #firstColumn="{ record }">
        <a>
          {{ record.viewCode }}
        </a>
      </template>
    </BasicTable>
    <BasicModal
      v-bind="$attrs"
      @register="registerModal"
      title="标签打印"
      @ok="handleModalInvClick"
      :width="500"
      :height="300"
      okText="下载文件"
      cancelText="全部关闭"
    >
      <div
        class="flex"
        style="
          justify-content: center;
          align-items: center;
          height: 100px;
          font-weight: bold;
          font-size: 20px;
        "
      >
        {{ fileName }}
      </div>
    </BasicModal>
    <barSizeTypeForm @register="registerBarSizeTypeModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasInvBasInvList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch, ref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, useTable } from '/@/components/Table';
  import {
    basInvDelete,
    basInvDisable,
    basInvEnable,
    basInvDisableBatch,
    basInvEnableBatch,
    basInvSyn,
    basInvListData,
  } from '/@/api/bas/inv/basInv';
  import { listSet, customListData } from '/@/api/test/testData';
  import { tabConfigData, schemasData } from '/@/utils/custom';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import { downloadByUrl } from '/@/utils/file/download';
  import { BarTypeEnum } from '/@/enums/defEnum';
  import barSizeTypeForm from './barSizeTypeForm.vue';

  const props = defineProps({
    treeCode: String,
    treeName: String,
  });
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
  });
  let title = ref('物料打印');
  let cinvcode = ref('');

  const { t } = useI18n('bas.inv.basInv');
  const { showMessage } = useMessage();

  const fileName = ref('');
  const pdfUrl = ref('');

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('物料代码'),
        field: 'viewCode',
        component: 'Input',
      },
      {
        label: t('物料名称'),
        field: 'invName',
        component: 'Input',
      },
      {
        label: t('规格型号'),
        field: 'invStd',
        component: 'Input',
      },
      // {
      //   label: t('物料分类'),
      //   field: 'invClsCode',
      //   fieldLabel: 'basInvCls.treeNames',
      //   component: 'TreeSelect',
      //   componentProps: {
      //     api: basInvClsTreeData,
      //   },
      // },
      {
        label: t('计量单位'),
        field: 'funitname',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('物料代码'),
      dataIndex: 'viewCode',
      key: 'a.view_code',
      sorter: true,
      width: 110,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('物料名称'),
      dataIndex: 'invName',
      key: 'a.inv_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'invStd',
      key: 'a.inv_std',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('物料分类'),
      dataIndex: 'basInvCls.name',
      key: 'a.inv_cls_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('客户图号'),
      dataIndex: 'f665',
      key: 'a.f665',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('计量单位'),
      dataIndex: 'funitname',
      key: 'a.funitname',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 120, //操作按钮宽度
    actions: (record: Recordable) => [
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除物料'),
        popConfirm: {
          title: t('是否确认删除物料'),
          confirm: handleDelete.bind(this, { invCode: record.invCode }),
        },
        auth: 'bas:inv:basInv:edit',
        ifShow: () => record.sourceType == '1',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用物料'),
        popConfirm: {
          title: t('是否确认停用物料'),
          confirm: handleDisable.bind(this, { invCode: record.invCode }),
        },
        auth: 'bas:inv:basInv:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用物料'),
        popConfirm: {
          title: t('是否确认启用启用'),
          confirm: handleEnable.bind(this, { invCode: record.invCode }),
        },
        auth: 'bas:inv:basInv:edit',
        ifShow: () => record.status === '2',
      },
    ],
  };

  const [registerBarSizeTypeModal, { openModal: openBarSizeTypeModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: basInvListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        invClsCode: props.treeCode,
        'basInvCls.treeNames': props.treeName,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await basInvDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 导入
  const [registerImportModal, { openModal: importModal }] = useModal();
  function handleImport() {
    importModal(true, {});
  }

  const [registerImportZlModal, { openModal: importZlModal }] = useModal();
  function handleImportZl() {
    importZlModal(true, {});
  }

  // 同步
  async function handleSyn() {
    const res = await basInvSyn({});
    showMessage(res.message);
    handleSuccess();
  }

  // 启用
  async function handleEnableInv() {
    if (getSelectRowKeys().length) {
      const res = await basInvEnableBatch({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一条数据');
    }
  }

  // 停用
  async function handleDisableInv() {
    if (getSelectRowKeys().length) {
      const res = await basInvDisableBatch({ selIds: getSelectRowKeys().join(',') });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一条数据');
    }
  }

  async function handleyfdhForm(record: Recordable) {
    openYfdhDrawer(true, {});
  }

  async function handleDisable(record: Recordable) {
    const res = await basInvDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await basInvEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handlePrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    openBarSizeTypeModal(true, { arr: arr, barSizeType: BarTypeEnum.Inventory });
  }

  function handleModalInvClick(record: Recordable) {
    // 下载
    downloadByUrl({ url: ctxPath + pdfUrl.value });
    closeModal();
  }

  function handleSuccess() {
    reload();
  }
</script>
